import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);


const state = {
	  userstel:["15025384658","18680816468","15823820938"],
	  flag:false,
	  // goods:localStorage["goods"]?JSON.parse(localStorage["goods"]): []
	  goods:[]
}

const getters = {
	  userstel(state){
	  	 return state.userstel 
	  },
	  flag(state){
	  	 return state.flag 
	  },
	  goods(state){
	  	 return state.goods
	  },
	  total(state){
			var total=0;
            state.goods.forEach((item)=>{
               if(item.select){
                    total+=item.price*item.num
                }   
            })
            return total
		},
		goodsNumber:state=>{
			return state.goods.length
		}
}

const actions = {  

}
const mutations={
	  toTrue(state){
	  		state.flag = true;
	  },
	  toFalse(state){
	  		state.flag = false;
	  },
	  addGoods:(state,data)=>{
			state.goods.push(data);
			// localStorage.setItem("goods",JSON.stringify(state.goods));					
		},
	  deleteGoods(state,index){
			state.goods.splice(index,1);
			// localStorage.setItem("goods",JSON.stringify(state.goods));
		},
	  updateGoods(state,data){
		const {index,key,value}=data;
		state.goods[index][key]=value;
		// localStorage.setItem("goods",JSON.stringify(state.goods));	
	 }
}
const store = new Vuex.Store({
       state,
       mutations,
       getters,
       actions
})


export default store